package lv.solcraft.replication.server.model;

import org.springframework.util.Assert;

import com.sleepycat.persist.model.KeyField;
import com.sleepycat.persist.model.Persistent;

@Persistent
public class Record {

    @KeyField(1)
    private String table;

    @KeyField(2)
    private Long recordSiteId;

    @KeyField(3)
    private Long recordId;

    /**
     * For persistence only
     */
    @Deprecated
    protected Record() {
    }

    public Record(String table, Long recordSiteId, Long recordId) {
        super();
        Assert.hasText(table, "Record table can not be null or empty");
        Assert.notNull(recordSiteId, "Record site id can not be null or empty");
        Assert.notNull(recordId, "Record id can not be null or empty");
        this.table = table;
        this.recordSiteId = recordSiteId;
        this.recordId = recordId;
    }

    public String getTable() {
        return table;
    }

    public Long getRecordSiteId() {
        return recordSiteId;
    }

    public Long getRecordId() {
        return recordId;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((recordId == null) ? 0 : recordId.hashCode());
        result = prime * result + ((recordSiteId == null) ? 0 : recordSiteId.hashCode());
        result = prime * result + ((table == null) ? 0 : table.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Record other = (Record) obj;
        if (recordId == null) {
            if (other.recordId != null)
                return false;
        } else if (!recordId.equals(other.recordId))
            return false;
        if (recordSiteId == null) {
            if (other.recordSiteId != null)
                return false;
        } else if (!recordSiteId.equals(other.recordSiteId))
            return false;
        if (table == null) {
            if (other.table != null)
                return false;
        } else if (!table.equals(other.table))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return table + ", " + recordSiteId + ":" + recordId;
    }

}
